package com.cx.service.impl;

import com.cx.mapper.ConcernMapper;
import com.cx.mapper.PostMapper;
import com.cx.mapper.UserMapper;
import com.cx.pojo.*;
import com.cx.service.UserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserCenterServiceImpl implements UserCenterService {
    @Autowired
    UserMapper userMapper;
    PostMapper postMapper;
    ConcernMapper concernMapper;

    @Override
    public User selectByPrimaryId(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

//    @Override
//    public Post selectByUserId(Integer id) {
//        Post post = postMapper.selectByUserId(id);
//        return post;
//    }

    @Override
    public String login(User user) {
        //校验该用户
        User userCheck = userMapper.selectByUser(user.getUserPhone());
        if (userCheck != null) {
            //密码登录
            if (userCheck.getUserPassword().equals(user.getUserPassword())) {
                user.setUserName(user.getUserPhone());
                String userStr = userCheck.getUserId() + ":" + user.getUserName();
                return userStr;
            } else {
                return "密码错误";
            }
        } else {
            return "用户不存在";
        }
    }

    @Override
    public String yzmlogin(User user) {
        //校验该用户
        User userCheck = userMapper.selectByUser(user.getUserPhone());
        if (userCheck != null) {
            //验证码登录
            user.setUserName(user.getUserPhone());
            String userStr = userCheck.getUserId() + ":" + user.getUserName();
            return userStr;

        } else {
            return "用户不存在";
        }
    }

    @Override
    public int insert(User user) {
//        user.setUserPhoto("");
        user.setUserName(user.getUserPhone());
        return userMapper.insert(user);
    }


    @Override
    public Integer updateByUserId(int userId, User user) {
        //通过criteria构造查询条件
        UserExample userExample = new UserExample();

        UserExample.Criteria criteria = userExample.createCriteria(); //构造自定义查询条件
        criteria.andUserIdEqualTo(userId);

        Integer rows = userMapper.updateByExampleSelective(user, userExample);

        return rows;
    }


    @Override
    public List<User> selectAll() {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        List<User> userList = userMapper.selectByExample(userExample);
        return userList;
    }

    //    删除用户
    @Override
    public Integer deleteUser(Integer userId) {
        Integer rows = userMapper.deleteByPrimaryKey(userId);
        return rows;
    }

    @Override
    public Integer updateConcernAdd(String userName) {
        Integer rows = userMapper.updateConcernAdd(userName);
        return rows;
    }

    @Override
    public Integer updateConcernDel(String userName) {
        Integer rows = userMapper.updateConcernDel(userName);
        return rows;
    }

    @Override
    public Integer updateFansAdd(String userName) {
        Integer rows = userMapper.updateFansAdd(userName);
        return rows;
    }

    @Override
    public Integer updateFansDel(String userName) {
        Integer rows = userMapper.updateFansDel(userName);
        return rows;
    }

    @Override
    public Integer updateStatusAdd(String userName) {
        Integer rows = userMapper.updateStatusAdd(userName);
        return rows;
    }

    @Override
    public Integer updateStatusDel(String userName) {
        Integer rows = userMapper.updateStatusDel(userName);
        return rows;
    }

    @Override
    public User selectByUsetName(String userName) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserNameEqualTo(userName);
        List<User> users = userMapper.selectByExample(userExample);
        return users.get(0);
    }

    @Override
    public Integer updateUserByUserName(String userName, User user) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserNameEqualTo(userName);
        Integer rows = userMapper.updateByExampleSelective(user, userExample);
        return rows;
    }

    @Override
    public String selectPhone(String userPhone) {
        return userMapper.selectPhone(userPhone);
    }


}
